home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / STANDALO / FUMBLE_F / FUMBLE_F.C next >
C/C++ Source or Header  |  1990-11-26  |  3KB  |  97 lines

  1. #include "SetupA4.h"
  2. #define TRAPNUM 0x01DC
  3.  
  4. /*** The following is my first attempt at an Init.  It was written using
  5.  *** Think C.  I make no promises that it's entirely correct, but it seems
  6.  *** to work.  Feel free to use any of this to make other interesting Inits.
  7.  *** This init watches for the string in the 'STR ' resource "Old String"
  8.  *** to be typed and replaces it with what's in the resource "New String".
  9.  ***
  10.  *** Send any comments to:
  11.  ***   Brad Quick Software
  12.  ***   PO Box 231
  13.  ***   Staatsburg, NY 12580
  14.  ***   GEnie: BSQ
  15.  ***/
  16.  
  17. typedef void (*fptr)();    /* pointer to a function */
  18.  
  19.  
  20. pascal void myproc(int,TEHandle);
  21.  
  22. long address; /*** stored address of TEKey trap ***/
  23. int pos;      /*** position in old string of next character we're waiting for ***/
  24. char oldstring[256]; /*** this is where we store the old string ***/
  25. char newstring[256]; /*** this is where we store the new string ***/
  26.  
  27. main()
  28.    {
  29.    Handle handle;
  30.    
  31.    RememberA0();
  32.    SetUpA4();
  33.    
  34.    /*** Detach this code resource so it stays where we can run it ***/
  35.    asm {
  36.        _RecoverHandle
  37.        move.l a0,handle
  38.        }
  39.    DetachResource(handle);
  40.    
  41.    /*** save old trap number in address ***/
  42.    address=GetTrapAddress(TRAPNUM);
  43.    
  44.    /*** set trap to my proc ***/
  45.    SetTrapAddress((long)&myproc,TRAPNUM);
  46.    
  47.    /*** load old string and new string ***/
  48.    handle=GetNamedResource('STR ',"\pOld String");
  49.    PtoCstr(*handle);
  50.    strcpy(oldstring,*handle);
  51.    ReleaseResource(handle);
  52.    
  53.    handle=GetNamedResource('STR ',"\pNew String");
  54.    PtoCstr(*handle);
  55.    strcpy(newstring,*handle);
  56.    ReleaseResource(handle);
  57.    
  58.    /*** start at the beginning of the string ***/
  59.    pos=0;
  60.  
  61.    RestoreA4();
  62.    }
  63.  
  64. pascal void myproc(c,h)
  65.    int c; /*** char variables are put on the stack as ints ***/
  66.    TEHandle h;
  67.    {
  68.    int x;
  69.    
  70.    SetUpA4(); /*** so we can access globals ***/
  71.    
  72.    /*** call the origional TEKey trap ***/   
  73.    /*** if returning a value, use CallPascalB,W,L see p.125 of Think C manual ***/
  74.    CallPascal(c,h,address);
  75.  
  76.    if (uppercase(c)==uppercase((int)oldstring[pos]))
  77.       {
  78.       ++pos;
  79.       if (oldstring[pos]=='\0') /*** we got the whole string ***/
  80.          {
  81.          /*** erase old string ***/
  82.          for (x=1;x<=strlen(oldstring);++x)
  83.             CallPascal(8,h,address); /*** backspace ***/
  84.  
  85.          /*** write the new string ***/
  86.          for (x=0;x<strlen(newstring);++x)
  87.             CallPascal((int)newstring[x],h,address);
  88.  
  89.          pos=0;
  90.          }
  91.       }
  92.    else pos=0;
  93.    
  94.    RestoreA4();
  95.    }
  96.    
  97.